//
//  HSAccessoryView.m
//  Wutuojia
//
//  Created by Work on 15/7/19.
//  Copyright (c) 2015年 Work. All rights reserved.
//

#import "HSAccessoryView.h"
#import "ALMacro.h"
#import "UIView+Frame.h"
//#import "HSShortOperationMacro.h"
#import "HSSelectView.h"
#import "ALTextField.h"
#import "UIColor+Extension.h"

@interface HSAccessoryView ()
{
    UIButton *_midBtn;
}
@property (nonatomic, unsafe_unretained) id<ALAccessoryViewDelegate> accessoryDelegate;
@end

@implementation HSAccessoryView

- (id)initWithDelegate:(id<ALAccessoryViewDelegate>)delegate
{
    self = [super init];
    if (self) {
        self.accessoryDelegate = delegate;
		[self setFrame:CGRectMake(0, 0, kScreenWidth, kToolBarHeight)];
		
		
        UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                                         style:UIBarButtonItemStyleBordered
                                                                        target:self
                                                                        action:@selector(inputCancel)];
        [cancelButton setTintColor:[UIColor colorWithHexString:@"65C4AA"]];

#warning 这里还需要更改
        _midBtn = [[UIButton alloc]
                       initWithFrame:(CGRect){
                           44,
                           0,
                           0,
                           kToolBarHeight}];
        [_midBtn setBackgroundColor:[UIColor clearColor]];
        [_midBtn setTitle:@"时间重置" forState:UIControlStateNormal];
        [_midBtn setTitleColor:[UIColor colorWithHexString:@"65C4AA"] forState:UIControlStateNormal];
        [_midBtn addTarget:self action:@selector(inputMid) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]
                                          initWithCustomView:_midBtn];
        
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"确认"
                                                                       style:UIBarButtonItemStyleBordered
                                                                      target:self
                                                                      action:@selector(inputDone)];
		[doneButton setTintColor:[UIColor colorWithHexString:@"65C4AA"]];
		if ([delegate isKindOfClass:[HSSelectView class]]) {
			[_midBtn setWidth:isAfterIOS8?kScreenWidth-44*2-5-5-10-10:kScreenWidth-44*2-15];
			UILabel *lbl=[[UILabel alloc] initWithFrame:(CGRect){
				44,
				0,
				5,
				kToolBarHeight}];
			UIBarButtonItem *flexibleSpace0 = [[UIBarButtonItem alloc]
											   initWithCustomView:lbl];
			self.items = @[flexibleSpace0,cancelButton,flexibleSpace,doneButton];
		}else{
			[_midBtn setWidth:isAfterIOS8?kScreenWidth-44*2-5-5-10:kScreenWidth-44*2-15];
			UIBarButtonItem *space0=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
				UIBarButtonItem *space1=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
				UIBarButtonItem *space2=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
				UIBarButtonItem *space3=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
			self.items = @[space0,cancelButton,space1,flexibleSpace,space2,doneButton,space3];
		}
		
        self.barStyle = UIBarStyleDefault;
        self.translucent = YES;
    }
    return self;
}


- (void)inputCancel{
    if (_accessoryDelegate && [_accessoryDelegate
                               respondsToSelector:@selector(accessoryViewDidPressedCancelButton:)]) {
        [_accessoryDelegate accessoryViewDidPressedCancelButton:self];
    }
}
- (void)inputMid{
    if (_accessoryDelegate && [_accessoryDelegate
                               respondsToSelector:@selector(accessoryViewDidPressedMidButton:)]) {
        [_accessoryDelegate accessoryViewDidPressedMidButton:self];
    }
}
- (void)inputDone{
    if (_accessoryDelegate && [_accessoryDelegate
                               respondsToSelector:@selector(accessoryViewDidPressedDoneButton:)]) {
        [_accessoryDelegate accessoryViewDidPressedDoneButton:self];
    }
}


@end
